# Use Ubuntu 20.04 as the base image
FROM ubuntu:20.04

# Set environment variables to avoid interactive prompts during package installation
ENV DEBIAN_FRONTEND=noninteractive

# Install system dependencies
RUN apt-get update && apt-get install -y \
    software-properties-common \
    wget \
    build-essential \
    libssl-dev \
    libffi-dev \
    libpq-dev \
    python3-dev \
    postgresql postgresql-contrib \
    && rm -rf /var/lib/apt/lists/*

# Add Python 3.10 PPA and install Python 3.10
RUN add-apt-repository ppa:deadsnakes/ppa -y && \
    apt-get update && apt-get install -y python3.10 python3.10-distutils python3.10-venv python3.10-dev && \
    rm -rf /var/lib/apt/lists/*

# Install pip for Python 3.10
RUN wget https://bootstrap.pypa.io/get-pip.py && \
    python3.10 get-pip.py && \
    rm get-pip.py

# Set Python 3.10 as the default Python version
RUN update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 1 && \
    update-alternatives --set python3 /usr/bin/python3.10

# Create the /app/noteagent directory
RUN mkdir -p /app/noteagent

# Copy the code and requirements file
COPY ./requirements.txt /app/noteagent/requirements.txt

# Set the working directory
WORKDIR /app/noteagent

# Install Python dependencies from requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Expose any necessary ports (e.g., for Flask)
EXPOSE 5000

# Initialize PostgreSQL and create user/database
RUN service postgresql start && \
    su - postgres -c "psql -c \"CREATE USER noteagent WITH PASSWORD 'noteagent';\"" && \
    su - postgres -c "psql -c \"CREATE DATABASE noteagent WITH OWNER noteagent;\"" && \
    service postgresql stop
COPY . /app/noteagent 
# Start PostgreSQL service and run application
CMD service postgresql start && python3 server.py 
#docker stop na1;docker rm na1
#docker build -f Dockerfile2 -t na .
#docker run -it --network host --name na1 na
#docker run -it --network host --name na1 na bash -c "service postgresql start && bash"
